<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  News Class
 *
 * @package XGO CMS v2.0
 * @subpackage News
 * @author Manh Nguyen - 30/07/2012
 * @link http://sunsoft.vn
*/
class Account extends CI_Controller {

	private $page_item = 5;

	/**
	 * Contrust class
	 *
	 * @access public
	 */
	public function __construct() {
		parent::__construct();
		$this->load->library('smarty3', '', 'smarty');
                $this->smarty = new CI_Smarty3();
		$this->load->database();
		$this->load->library('datamapper');
		$this->load->library('session');
                $this->load->library('247mall/mall_service');
                $this->load->model('cart_model');
		$this->load->helper('url');
                $this->client_ip = getIP();
                
                require_once APPPATH.'modules/frontend/common/top.php';
                require_once (APPPATH.'modules/frontend/common/main_menu.php');
                require_once APPPATH.'modules/frontend/common/footer.php';
                require_once (APPPATH.'../data/captcha/securimage.php');
	}

	public function index() {
            //$this->session->set_flashdata('redirect', $this->uri->uri_string());
            //$this->smarty->assign('redirect', $this->session->flashdata('redirect'));
            $this->smarty->display('template/frontend/account');
        }
        
        public  function users()
        {
            $a = new Mall_service();
            //$a->isEmailExist("test12@gmail.com", $this->client_ip);
            print_r($a->getAllUser($this->client_ip));exit;
        }

        public function login(){
            $email = $this->input->post('username');
            
            $password = md5($this->input->post('password'));
            $msg=array();
            if (preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $email) === 0) {
                $code = -1;
                $msg[] = "Email không hợp lệ hoặc chưa đúng định dạng.";
            }
            
            if(count($msg)==0)
            {
                $mall = new Mall_service();
                $result = $mall->checkLogin($email, $password, $this->client_ip);
                $json = array('result'=>$result['code'],'message'=>$result['msg']);
                
                if($result['code']==1)
                {
                    $user_info = json_decode($result['msg']);
                    $_SESSION['user_info'] = $user_info;
                    //$this->session->set_userdata('user_info', $user_info);//print_r($this->session->userdata('user_info'));exit;
                }
                //print_r($user);exit;
            }
            else
                $json = array('result'=>$code,'message'=>$msg);
            echo json_encode($json);
            
        }
        
        public function register(){
            //$uname = explode('@',$this->input->post('username'));
            $username = time();
            $password = md5($this->input->post('password'));
            $email = $this->input->post('username');
            $captcha = $this->input->post('captcha');
            $securimage = new Securimage();
            
            $msg=array();
            
            if (!$username || strlen($username) > 20 || strlen($username) < 6) {
                $code = -1;
                $msg[] = "Tên đăng nhập phải từ 6-20 ký tự.";
            } elseif (!preg_match('/^[a-z0-9._]+$/', $username)) {
                $code = -1;
                $msg[] = "Tên đăng nhập chỉ cho phép các ký tự (a – z), số (0 – 9) và dấu chấm(.).";
            }
            if (!$password || strlen($password) < 6) {
                $code = -1;
                $msg[] = "Mật khẩu quá ngắn (trên 5 ký tự).";
            }
            if (preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $email) === 0) {
                $code = -1;
                $msg[] = "Email không hợp lệ hoặc chưa đúng định dạng.";
            }
            if (strlen($password) >= 6) {
                if ($password == $username)
                    $msg[] = "Mật khẩu không được trùng với Tài khoản.";
            }
            if (!$securimage->check($captcha) && $captcha!==false) {
                $code = -1;
                $msg[] = "Mã xác nhận không đúng.";
            }
            
            if(count($msg)==0)
            {
                $mall = new Mall_service();
                $result = $mall->quickRegister($username, $password, $email, $this->client_ip);
                
                if(is_array($result))
                {
                    $code = $result['code'];
                    $msg = $result['msg'];
                    $json = array("result"=>$code,"message"=>$msg);
                    
                    if($result['code']==1)
                    {
                        $user_info = json_decode($result['msg']);
                        $_SESSION['user_info'] = $user_info;
                        //$this->session->set_userdata('user_info', $user_info);
                    }
                }
                else
                    $json = $result;
            }
            else
                $json = array("result"=>$code,"message"=>$msg);
            echo json_encode($json);
            
        }
        
        public function donothing(){
            
        }
        
        public  function logout()
        {
            session_destroy();
            $this->session->sess_destroy();
            redirect($this->session->flashdata('redirect'));
        }
}

/* End of file news.php */
/* Location: ./system/application/controllers/news.php */